//
//  TKYTabBarController.m
//  常用的类和基类
//
//  Created by apple on 2018/3/16.
//  Copyright © 2018年 铁科院. All rights reserved.
//

#import "TKYTabBarController.h"
#import "TKYTabbar.h"
//#import "TKYAnalysisController.h"
#import "TKYHomePageController.h"
#import "TKYMineController.h"
#import "TKYFileController.h"
#import "TKYNavigetionController.h"
#import "TKYProgressController.h"
#import "TKYAddCenterView.h"
#import "TKYAddQualityController.h"
#import "TKYAddQualityMessageController.h"
#import "TKYAddSafetyController.h"
#import "TKYTabbar+TKYbadge.h"
#import "TKYTreatHandelController.h"
@interface TKYTabBarController ()<TKYTabBarDelegate> 
    //实现自定义TabBar协议

@end

@implementation TKYTabBarController

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    /**** 设置所有UITabBarItem的文字属性 ****/
    [self setupItemTitleTextAttributes];
    
    /**** 添加子控制器 ****/
    [self setupChildViewControllers];
    
    /**** 更换TabBar ****/
    [self setupTabBar];
    
    self.view.backgroundColor = kMainColour;

}

/**
 *  设置所有UITabBarItem的文字属性
 */
- (void)setupItemTitleTextAttributes
{
    UITabBarItem *item = [UITabBarItem appearance];
    // 普通状态下的文字属性
    NSMutableDictionary *normalAttrs = [NSMutableDictionary dictionary];
    normalAttrs[NSFontAttributeName] = [UIFont systemFontOfSize:10];
    normalAttrs[NSForegroundColorAttributeName] = kUIColorFromRGB(666666, 1);
    [item setTitleTextAttributes:normalAttrs forState:UIControlStateNormal];
    // 选中状态下的文字属性
    NSMutableDictionary *selectedAttrs = [NSMutableDictionary dictionary];
    selectedAttrs[NSForegroundColorAttributeName] = KNavColor;
    [item setTitleTextAttributes:selectedAttrs forState:UIControlStateSelected];
}

/**
 *  添加子控制器
 */
- (void)setupChildViewControllers
{
    [self setupOneChildViewController:[[TKYNavigetionController alloc] initWithRootViewController:[[TKYHomePageController alloc] init]] title:@"首页" image:@"home" selectedImage:@"home_selected" badgeValue:nil];
    
    [self setupOneChildViewController:[[TKYNavigetionController alloc] initWithRootViewController:[[TKYFileController alloc] init]] title:@"文档" image:@"file" selectedImage:@"" badgeValue:nil];
    
    [self setupOneChildViewController:[[TKYNavigetionController alloc] initWithRootViewController:[[TKYTreatHandelController alloc] init]] title:@"待办" image:@"analysis" selectedImage:@"analysis_selected" badgeValue:nil];
    
    [self setupOneChildViewController:[[TKYNavigetionController alloc] initWithRootViewController:[[TKYMineController alloc] init]] title:@"我的" image:@"member" selectedImage:@"member_selected" badgeValue:nil];
    
}

/**
 *  初始化一个子控制器
 *
 *  @param vc            子控制器
 *  @param title         标题
 *  @param image         图标
 *  @param selectedImage 选中的图标
 */
- (void)setupOneChildViewController:(UIViewController *)vc title:(NSString *)title image:(NSString *)image selectedImage:(NSString *)selectedImage badgeValue:(NSString *)badgeValue
{
    vc.tabBarItem.title = title;
    vc.tabBarItem.badgeValue = badgeValue;
    if (image.length) { // 图片名有具体值
        vc.tabBarItem.image = [UIImage imageNamed:image];
        vc.tabBarItem.selectedImage = [UIImage imageNamed:selectedImage];
    }
    [self addChildViewController:vc];
}

#pragma mark - MyTabBarDelegate
-(void)addButtonClick:(TKYTabbar *)tabBar
 {
    
     self.addCenterView = [[TKYAddCenterView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
     __weak typeof(self) weakSelf = self;
     self.addCenterView.touchBgView = ^{
         [weakSelf.addCenterView removeFromSuperview];

     };

     self.addCenterView.progressBtnBlock = ^(UIButton *progressBtn) {
         NSLog(@"填报进度");
         TKYProgressController *progressVC = [[TKYProgressController alloc]init];
         //       addVC.view.backgroundColor = kMainColour;
        TKYProgressController *nav = [[TKYNavigetionController alloc]initWithRootViewController:progressVC];
         [weakSelf.addCenterView removeFromSuperview];
         [weakSelf presentViewController:nav animated:YES completion:nil];
     };

     self.addCenterView.qualityBtnBlock = ^(UIButton *progressBtn) {
         TKYAddQualityMessageController *addQualityVC = [[TKYAddQualityMessageController alloc]init];
         TKYAddQualityMessageController *nav = [[TKYNavigetionController alloc]initWithRootViewController:addQualityVC];
         [weakSelf.addCenterView removeFromSuperview];

         [weakSelf presentViewController:nav animated:YES completion:nil];
          NSLog(@"质量问题");
     };
     self.addCenterView.safetysBtnBlock  = ^(UIButton *progressBtn) {
         NSLog(@"安全问题");
         TKYAddSafetyController *safetyVC = [[TKYAddSafetyController alloc]init];
         TKYAddSafetyController *nav = [[TKYNavigetionController alloc]initWithRootViewController:safetyVC];
         [weakSelf.addCenterView removeFromSuperview];
           [weakSelf presentViewController:nav animated:YES completion:nil];
     };

     self.addCenterView.closeBtnBlock = ^(UIButton *closeBtn) {
          [weakSelf.addCenterView removeFromSuperview];
     };
     [self.view addSubview:self.addCenterView];
}

/**
 *  更换TabBar
 */
- (void)setupTabBar
{
    //创建自定义TabBar
    TKYTabbar *myTabBar = [[TKYTabbar alloc] init];
    myTabBar.TKYTabBarDelegate = self;
    self.myTabBar = myTabBar;

    [self setValue:myTabBar forKeyPath:@"tabBar"];
    //获取通知中心单例对象
    NSNotificationCenter * center = [NSNotificationCenter defaultCenter];
    //添加当前类对象为一个观察者，name和object设置为nil，表示接收一切通知
    [center addObserver:self selector:@selector(notice:) name:@"123" object:nil];
    [myTabBar showBadgeOnItemIndex:4];
}

-(void)notice:(id)sender{
    [self.myTabBar hideBadgeOnItemIndex:4];
}



@end
